//
//  HNHappyTimeController.m
//  XDEducation
//
//  Created by 星道 on 17/8/2.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNHappyTimeController.h"
#import "HNHappyTimeSectionModel.h"
#import "HNHappyTimeQunModel.h"
#import "HNHappyTimeModel.h"
#import "HNHappyTimeSectionView.h"
#import "HNHappyTimeCell.h"
#import "HNHappyTimeTalkVC.h"

@interface HNHappyTimeController ()<UITableViewDelegate,UITableViewDataSource>
{
    IBOutlet UITableView *_TableView;
    /** 菊花 */
    IBOutlet UIActivityIndicatorView *_Activity;
}
/** 头部的数组 */
@property (nonatomic, strong) NSArray <HNHappyTimeSectionModel *>*SectionArr;
/** 群组成员 */
@property (nonatomic, strong) NSMutableArray <HNHappyTimeQunModel *>*QunArr;
@property (nonatomic, strong) NSArray <HNHappyTimeQunModel *>*QunTempArr;
/** 家庭成员 */
@property (nonatomic, strong) NSMutableArray <HNHappyTimeModel *>*FamilyArr;
@property (nonatomic, strong) NSArray <HNHappyTimeModel *>*FamilyTempArr;
/** 老师成员 */
@property (nonatomic, strong) NSMutableArray <HNHappyTimeModel *>*TeacherArr;
@property (nonatomic, strong) NSArray <HNHappyTimeModel *>*TeacherTempArr;
@end

@implementation HNHappyTimeController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [_TableView registerNib:[UINib nibWithNibName:[HNHappyTimeCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];

    [self RefreshDataContacts];
}

#pragma makr - 网络请求
/** 获取全部联系人 */
- (void)RefreshDataContacts
{
    NSDictionary *Params = @{
                             @"uid":[AccountTool shareAccountTool].currentAccount.uid,
                             @"action":@"info"
                             };
    [XDHttpTool get:BaseURL(@"chat.do?") authCode:[AccountTool shareAccountTool].currentAccount.authCode params:Params success:^(id responseObj) {

        if (responseObj)
        {
            // 停止菊花转动
            [_Activity removeFromSuperview];
            // 群成员
            [responseObj[@"myqun"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [self.QunArr addObject:[HNHappyTimeQunModel mj_objectWithKeyValues:obj]];
            }];
            // 家人成员
            [responseObj[@"myFamily"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [self.FamilyArr addObject:[HNHappyTimeModel mj_objectWithKeyValues:obj]];
            }];
            // 老师成员
            [responseObj[@"myTeacher"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [self.TeacherArr addObject:[HNHappyTimeModel mj_objectWithKeyValues:obj]];
            }];

            // 表单显示
            _TableView.hidden = NO;
            [_TableView reloadData];
        }
        else
        {
            [_Activity removeFromSuperview];
        }
    } failure:^(NSError *error) {
        [_Activity removeFromSuperview];
    }];
}
#pragma mark - TableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.SectionArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    HNHappyTimeSectionModel *Model = self.SectionArr[section];
    switch (section)
    {
        case 0:return Model.IsOpen ? self.FamilyArr.count:0;break;   // 家庭成员
        case 1:return Model.IsOpen ? self.TeacherArr.count:0;break;  // 老师成员
        case 2:return Model.IsOpen ? self.QunArr.count:0;break;      // 群成员
        default:return 0;break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 实例化
    HNHappyTimeCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    // 我的一家
    if (indexPath.section == 0)
    {
        HNHappyTimeModel *Model = self.FamilyArr[indexPath.row];
        Cell.Model = Model;

    }
    // 我的老师
    else if (indexPath.section == 1)
    {
        HNHappyTimeModel *Model = self.TeacherArr[indexPath.row];
        Cell.Model = Model;
    }
    // 亲人群组
    else
    {
        HNHappyTimeQunModel *Model = self.QunArr[indexPath.row];
        Cell.QunModel = Model;
    }
    return Cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[HNHappyTimeSectionView alloc] initWithModel:self.SectionArr[section] Identifier:@"SectionView" Select:^(HNHappyTimeSectionModel *Model) {
        [self SetCellStatusWithModel:Model];
        [_TableView reloadData];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HNHappyTimeTalkVC *VC = [[HNHappyTimeTalkVC alloc] init];
    // 我的一家
    if (indexPath.section == 0)
    {
        HNHappyTimeModel *Model = self.FamilyArr[indexPath.row];
        VC.Name = Model.nickName;
        VC.Uid = Model.uid;
    }

    // 我的老师
    if (indexPath.section == 1)
    {
        HNHappyTimeModel *Model = self.TeacherArr[indexPath.row];
        VC.Name = Model.nickName;
        VC.Uid = Model.uid;
    }

    // 亲人群组
    if (indexPath.section == 2)
    {
        HNHappyTimeQunModel *Model = self.QunArr[indexPath.row];
        VC.Name = Model.qun_name;
        VC.Uid = Model.qid;
    }

    [self.navigationController pushViewController:VC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

#pragma mark - 方法
/** section的打开或关闭使用这个方法 */
- (void)SetCellStatusWithModel:(HNHappyTimeSectionModel *)Model
{
    // 设置打开或关闭
    Model.IsOpen = !Model.IsOpen;
    // 我的一家
    if ([Model.ID isEqualToNumber:@1])
    {
        if (Model.IsOpen)
        {
            [self.FamilyArr addObjectsFromArray:self.FamilyTempArr];
        }
        else
        {
            self.FamilyTempArr = [self.FamilyArr copy];
            [self.FamilyArr removeAllObjects];
        }
    }
    // 我的老师
    if ([Model.ID isEqualToNumber:@2])
    {
        if (Model.IsOpen)
        {
            [self.TeacherArr addObjectsFromArray:self.TeacherTempArr];
        }
        else
        {
            self.TeacherTempArr = [self.TeacherArr copy];
            [self.TeacherArr removeAllObjects];
        }
    }
    // 我的亲人
    if ([Model.ID isEqualToNumber:@3])
    {
        if (Model.IsOpen)
        {
            [self.QunArr addObjectsFromArray:self.QunTempArr];
        }
        else
        {
            self.QunTempArr = [self.QunArr copy];
            [self.QunArr removeAllObjects];
        }
    }

}


#pragma mark - Setter
- (NSArray<HNHappyTimeSectionModel *> *)SectionArr
{
    if (!_SectionArr)
    {
        HNHappyTimeSectionModel *Model1 = [[HNHappyTimeSectionModel alloc] initWithID:@1 Title:@"我的一家" IsOpen:NO];
        HNHappyTimeSectionModel *Model2 = [[HNHappyTimeSectionModel alloc] initWithID:@2 Title:@"我的老师" IsOpen:NO];
        HNHappyTimeSectionModel *Model3 = [[HNHappyTimeSectionModel alloc] initWithID:@3 Title:@"亲人群组" IsOpen:NO];
        _SectionArr = @[Model1,Model2,Model3];
    }
    return _SectionArr;
}

- (NSMutableArray<HNHappyTimeModel *> *)FamilyArr
{
    if (!_FamilyArr)
    {
        _FamilyArr = [NSMutableArray array];
    }
    return _FamilyArr;
}

- (NSMutableArray<HNHappyTimeModel *> *)TeacherArr
{
    if (!_TeacherArr)
    {
        _TeacherArr = [NSMutableArray array];
    }
    return _TeacherArr;
}

- (NSMutableArray<HNHappyTimeQunModel *> *)QunArr
{
    if (!_QunArr)
    {
        _QunArr = [NSMutableArray array];
    }
    return _QunArr;
}
@end
